package com.tiger.ejbql.sample1;

import java.util.*;
import javax.ejb.*;
import javax.naming.*;

public class EJBQLTest1Bean implements javax.ejb.SessionBean {
    private javax.ejb.SessionContext sessionContext = null;
    
    public void setSessionContext(javax.ejb.SessionContext sessionContext) {
        this.sessionContext = sessionContext;
    }
    
    public void createTestData() {
        try {
            InitialContext ctx = new InitialContext();
            EJBQLA1Home aHome = (EJBQLA1Home)ctx.lookup("java:comp/env/ejb/EJBQLA1Bean");
            EJBQLB1Home bHome = (EJBQLB1Home)ctx.lookup("java:comp/env/ejb/EJBQLB1Bean");
            EJBQLC1Home cHome = (EJBQLC1Home)ctx.lookup("java:comp/env/ejb/EJBQLC1Bean");
            
            // テストデータの作成
            EJBQLA1 a1 = aHome.create(new Integer(1), "A1");
            EJBQLA1 a2 = aHome.create(new Integer(2), "A1");
            EJBQLA1 a3 = aHome.create(new Integer(3), "A2");
            EJBQLA1 a4 = aHome.create(new Integer(4), "A1");
            EJBQLA1 a5 = aHome.create(new Integer(5), "A1");
            
            EJBQLB1 b1 = bHome.create(new Integer(1), "B1");
            EJBQLB1 b2 = bHome.create(new Integer(2), "B1");
            EJBQLB1 b3 = bHome.create(new Integer(3), "B1");
            EJBQLB1 b4 = bHome.create(new Integer(4), "B1");
            EJBQLB1 b5 = bHome.create(new Integer(5), "B2");
            
            EJBQLC1 c1 = cHome.create(new Integer(1), "C1");
            EJBQLC1 c2 = cHome.create(new Integer(2), "C1");
            EJBQLC1 c3 = cHome.create(new Integer(3), "C2");
            EJBQLC1 c4 = cHome.create(new Integer(4), "C1");
            
            a1.setEjbqlb1(b1);
            b1.setEjbqlc1(c1);
            
            a2.setEjbqlb1(b2);
            
            a3.setEjbqlb1(b3);
            b3.setEjbqlc1(c2);
            
            a4.setEjbqlb1(b4);
            b4.setEjbqlc1(c3);
            
            a5.setEjbqlb1(b5);
            b5.setEjbqlc1(c4);
        }
        catch(javax.naming.NamingException e) {
            throw new javax.ejb.EJBException(e);
        }
        catch(javax.ejb.CreateException e) {
            throw new javax.ejb.EJBException(e);
        }
    }
    
    public void test() {
        try {
            InitialContext ctx = new InitialContext();
            EJBQLA1Home aHome = (EJBQLA1Home)ctx.lookup("java:comp/env/ejb/EJBQLA1Bean");
            EJBQLB1Home bHome = (EJBQLB1Home)ctx.lookup("java:comp/env/ejb/EJBQLB1Bean");
            EJBQLC1Home cHome = (EJBQLC1Home)ctx.lookup("java:comp/env/ejb/EJBQLC1Bean");
            
            System.out.println("findByNames1@BHome:");
            Iterator namesB1Results = bHome.findByNames1("A1", "B1").iterator();
            while(namesB1Results.hasNext()) {
                EJBQLB1 b = (EJBQLB1)namesB1Results.next();
                System.out.println("id = " + b.getId() +
                " name = " + b.getName());
            }
            System.out.println();
            System.out.println("findByNames2@BHome:");
            Iterator namesB2Results = bHome.findByNames2("A1", "B1").iterator();
            while(namesB2Results.hasNext()) {
                EJBQLB1 b = (EJBQLB1)namesB2Results.next();
                System.out.println("id = " + b.getId() +
                " name = " + b.getName());
            }
            System.out.println();
            System.out.println("findByNames3@BHome:");
            Iterator namesB3Results = bHome.findByNames3("A1", "B1").iterator();
            while(namesB3Results.hasNext()) {
                EJBQLB1 b = (EJBQLB1)namesB3Results.next();
                System.out.println("id = " + b.getId() +
                " name = " + b.getName());
            }
            
            System.out.println();
            System.out.println("findByNames1@CHome:");
            Iterator namesC1Results = cHome.findByNames1("A1", "B1", "C1").iterator();
            while(namesC1Results.hasNext()) {
                EJBQLC1 c = (EJBQLC1)namesC1Results.next();
                System.out.println("id = " + c.getId() +
                " name = " + c.getName());
            }
            System.out.println();
            System.out.println("findByNames2@CHome:");
            Iterator namesC2Results = cHome.findByNames2("A1", "B1", "C1").iterator();
            while(namesC2Results.hasNext()) {
                EJBQLC1 c = (EJBQLC1)namesC2Results.next();
                System.out.println("id = " + c.getId() +
                " name = " + c.getName());
            }
            System.out.println();
            System.out.println("findByNames3@CHome:");
            Iterator namesC3Results = cHome.findByNames3("A1", "B1", "C1").iterator();
            while(namesC3Results.hasNext()) {
                EJBQLC1 c = (EJBQLC1)namesC3Results.next();
                System.out.println("id = " + c.getId() +
                " name = " + c.getName());
            }
        }
        catch(javax.naming.NamingException e) {
            throw new javax.ejb.EJBException(e);
        }
        catch(javax.ejb.FinderException e) {
            throw new javax.ejb.EJBException(e);
        }
    }
    
    public void ejbCreate() {}
    
    public void ejbActivate() {}
    
    public void ejbPassivate() {}
    
    public void ejbRemove() {}
}
